package app.kotlin_1.fragment

import android.app.Activity
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v4.view.PagerAdapter
import android.support.v4.view.ViewPager
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import app.kotlin_1.R
import app.kotlin_1.activity.SearchActivity
import app.kotlin_1.adapter.HomeDrawerLayoutAdapter
import app.kotlin_1.adapter.HomeGameKindAdapter
import app.kotlin_1.adapter.HomeNewGameAdapter
import app.kotlin_1.base.BaseFragment
import app.kotlin_1.bean.GameBean
import app.kotlin_1.bean.GameKindBean
import app.kotlin_1.utils.ActivityUtils
import app.kotlin_1.view.MyItemDecoration
import com.facebook.drawee.view.SimpleDraweeView
import kotlinx.android.synthetic.main.fragment_home.view.*
import kotlinx.android.synthetic.main.title_home_fragment.view.*
import kotlinx.android.synthetic.main.view_home_drawerlayout.view.*
import kotlinx.android.synthetic.main.view_home_fragment.view.*

/**
 * Created by 刘智强 on 2017/9/19.
 */
class HomeFragment : BaseFragment() {

    private lateinit var vp_banner: ViewPager
    private lateinit var rv_kind: RecyclerView
    private val viewList = mutableListOf<View>()
    private var kindList = mutableListOf<String>()
    private val newGameList = mutableListOf<GameBean>()
    private val hotGameList = mutableListOf<GameBean>()
    private val kindDrawerLayoutList = mutableListOf<GameKindBean>()
    private var homeAdapter: HomeGameKindAdapter? = null
    private var newGameAdapter: HomeNewGameAdapter? = null
    private var hotGameAdapter: HomeNewGameAdapter? = null;
    private var homeDrawerLayoutAdapter: HomeDrawerLayoutAdapter? = null;
    private var isOpen: Boolean = false

    companion object {
        fun newIntance(args: String): HomeFragment? {
            val fragment = HomeFragment()
            val bundle: Bundle = Bundle()
            bundle!!.putString("args", args)
            fragment.arguments = bundle
            return fragment
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.fragment_home
    }

    override fun initData() {
        var sv_1 = SimpleDraweeView(activity)
        sv_1.setImageURI("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1506413113&di=b9839770976ae82cd9eebc9195f51636&imgtype=jpg&er=1&src=http%3A%2F%2Fpic.58pic.com%2F58pic%2F15%2F16%2F30%2F59c58PICQpf_1024.jpg")
        var sv_2 = SimpleDraweeView(activity)
        sv_2.setImageURI("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1505818393981&di=2b41cd22b674b22829261f683b4eef4f&imgtype=0&src=http%3A%2F%2Fpic5.duowan.com%2Fwow%2F0908%2F114846893719%2F114848423086.jpg")
        var sv_3 = SimpleDraweeView(activity)
        sv_3.setImageURI("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1505818393981&di=039ed0caaea0f3690c4e6d8853d3f2f6&imgtype=0&src=http%3A%2F%2Fattach.bbs.miui.com%2Fforum%2F201306%2F09%2F103155bqvss11iiv2vsc13.jpg")
        var sv_4 = SimpleDraweeView(activity)
        sv_4.setImageURI("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1505818393981&di=8446d602f881b1bed0fcff7e8b6998f3&imgtype=0&src=http%3A%2F%2Fimg10.3lian.com%2Fc1%2Fnewpic%2F10%2F24%2F34.jpg")
        viewList.add(sv_1)
        viewList.add(sv_2)
        viewList.add(sv_3)
        viewList.add(sv_4)
        kindList.add("热门活动")
        kindList.add("游戏最前线")
        kindList.add("新游评测")
        kindList.add("游戏攻略")
        kindList.add("咨询新闻")
        kindList.add("原创娱乐")

        newGameList.add(GameBean("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1505888477527&di=40719d4c0453c8ef489d314cdb2772b0&imgtype=0&src=http%3A%2F%2Fimg.18183.com%2Fuploads%2F170901%2F154-1FZ1155RC18.jpg", "重返部落"))
        newGameList.add(GameBean("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1505888550452&di=b009a48dbc9cbe3489b6024411a686cd&imgtype=0&src=http%3A%2F%2Fstatic.ewoka.com%2Fuploads%2Fallimg%2F170824%2F724258_170824180752_1.jpg", "爆蛋军团"))
        newGameList.add(GameBean("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1506483384&di=44d34d71207d7907d271354ce7702b2d&imgtype=jpg&er=1&src=http%3A%2F%2Fimage.game.uc.cn%2F2016%2F5%2F26%2F13277166.jpg", "青龙战纪"))
        newGameList.add(GameBean("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1506483414&di=435ea05d4cc86c2f7939f3ad595bd87c&imgtype=jpg&er=1&src=http%3A%2F%2Fwww.1688wan.com%2Fallimgs%2Fimage%2F2016%2F05%2F31%2F20160531181747_490.jpg", "自创武林"))
        newGameList.add(GameBean("https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=3061366639,147781261&fm=27&gp=0.jpg", "曙光破晓"))
        newGameList.add(GameBean("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1506483516&di=88ece13a04ce08cfacb3dfee32cf5783&imgtype=jpg&er=1&src=http%3A%2F%2Fimg.9553.com%2Fuploadfile%2F2016%2F0910%2F20160910044818947.jpg", "剑与家园"))

        hotGameList.add(GameBean("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1505897667918&di=b9bbab96913ba0fa1d50cf02b11fa2da&imgtype=0&src=http%3A%2F%2Fimg2.gtimg.com%2Fdbimg%2F2013%2Fmainpiclib%2F20131126%2F16%2F7dc28658e59b9a3d3de63360e1a1dcb22.jpg", "炉石传说"))
        hotGameList.add(GameBean("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1505897726144&di=f94334daf1b49e104d839fb7fedb5b89&imgtype=0&src=http%3A%2F%2Fimg1.gamedog.cn%2F2013%2F08%2F27%2F95-130RFU9110.jpg", "轩辕传奇"))
        hotGameList.add(GameBean("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1506492497&di=5f4a7ccb3c2b8d255df0274674426144&imgtype=jpg&er=1&src=http%3A%2F%2Fwww.3dmgame.com%2Fuploads%2Fallimg%2F161215%2F346_161215174407_1.jpg", "阴阳师"))
        hotGameList.add(GameBean("https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2099374589,3947003001&fm=11&gp=0.jpg", "梦幻西游"))
        hotGameList.add(GameBean("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1506492604&di=dab1c6279dd0260707180b60c71fbe63&imgtype=jpg&er=1&src=http%3A%2F%2Fnewsimg.5054399.com%2Fuploads%2Fuserup%2F1610%2F1111364W3D.jpg", "王者荣耀"))
        hotGameList.add(GameBean("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1506492657&di=bf2770ceedb9de46bc1bfbd856db60b7&imgtype=jpg&er=1&src=http%3A%2F%2Fimg.xskhome.com%2Fpic%2F2017%2F08%2F20170805%2Fclooect%2F173651_1623889403.jpg", "我的世界"))

        kindDrawerLayoutList.add(GameKindBean(R.drawable.juesebanyan, "角色扮演"))
        kindDrawerLayoutList.add(GameKindBean(R.drawable.dongzuosheji, "动作射击"))
        kindDrawerLayoutList.add(GameKindBean(R.drawable.tiyujingji, "体育竞技"))
        kindDrawerLayoutList.add(GameKindBean(R.drawable.qipaizhuoyou, "棋牌桌游"))
        kindDrawerLayoutList.add(GameKindBean(R.drawable.jingyingcelue, "经营策略"))
        kindDrawerLayoutList.add(GameKindBean(R.drawable.xiuxianyizhi, "休闲益智"))

//初始化首页分类的数据
        rv_kind.layoutManager = GridLayoutManager(activity, 3)
        rv_kind.addItemDecoration(MyItemDecoration(activity, MyItemDecoration.BOTH_SET, 10, Color.LTGRAY))
        homeAdapter = HomeGameKindAdapter(activity, kindList)
        rv_kind.adapter = homeAdapter
        homeAdapter?.notifyDataSetChanged()
        //初始化首页最新游戏的数据

        rootView!!.rvNew.layoutManager = GridLayoutManager(activity, 3)
        rootView!!.rvNew.addItemDecoration(MyItemDecoration(activity, MyItemDecoration.BOTH_SET, 5, Color.BLUE))
        newGameAdapter = HomeNewGameAdapter(activity, newGameList)
        rootView!!.rvNew.adapter = newGameAdapter
        newGameAdapter!!.notifyDataSetChanged()
        //初始化首页最热游戏的数据
        rootView!!.rvHot.layoutManager = GridLayoutManager(activity, 3)
        rootView!!.rvHot.addItemDecoration(MyItemDecoration(activity, MyItemDecoration.BOTH_SET, 5, Color.RED))
        hotGameAdapter = HomeNewGameAdapter(activity, hotGameList)
        rootView!!.rvHot.adapter = hotGameAdapter
        newGameAdapter!!.notifyDataSetChanged()
        //初始化侧滑栏的数据
        rootView!!.rvDrawerLayoutKind.layoutManager = LinearLayoutManager(activity)
        rootView!!.rvDrawerLayoutKind.addItemDecoration(MyItemDecoration(activity, MyItemDecoration.HORIZONTAL_LIST, 5, Color.BLACK))
        homeDrawerLayoutAdapter = HomeDrawerLayoutAdapter(activity, kindDrawerLayoutList)
        rootView!!.rvDrawerLayoutKind.adapter = homeDrawerLayoutAdapter
        homeDrawerLayoutAdapter!!.notifyDataSetChanged()

        initDrawerLayout()
        initSearch()
    }

    /**
     * 初始化侧滑栏
     */
    fun initDrawerLayout() {

        rootView!!.ivKind.setOnClickListener { v ->
            run {
                ActivityUtils.getActivity()
                if (isOpen) {
                    isOpen = false
                    rootView!!.dlMenu.closeDrawer(Gravity.LEFT)
                } else {
                    isOpen = true
                    rootView!!.dlMenu.openDrawer(Gravity.LEFT)
                }

            }
        }

    }

   private fun initSearch() {
        rootView!!.ivSearch.setOnClickListener { v ->
            startActivity(Intent(activity, SearchActivity::class.java))

        }
    }

    override fun initView(v: View) {
        vp_banner = v.findViewById(R.id.vp_banner) as ViewPager
        rv_kind = v.findViewById(R.id.rv_kind) as RecyclerView

    }

    override fun initListener() {
        vp_banner.adapter = object : PagerAdapter() {
            override fun isViewFromObject(view: View?, `object`: Any?): Boolean {
                return view == `object`

            }

            override fun getCount(): Int {
                return viewList.size

            }

            override fun destroyItem(container: ViewGroup?, position: Int, `object`: Any?) {
                container!!.removeView(viewList.get(position))
            }

            override fun instantiateItem(container: ViewGroup?, position: Int): Any {
                container!!.addView(viewList.get(position))
                return viewList.get(position)
            }

        }


    }


    override fun setOnClick(v: View?) {
    }

    override fun requestData() {
    }

    override fun setData() {
    }

    override fun onResume() {
        super.onResume()
    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        if (hidden&&isOpen){
            rootView!!.dlMenu.openDrawer(Gravity.LEFT)
        }
    }


}
